package mall.controller.client;

import mall.core.web.Controller;
import mall.entity.Order;
import mall.entity.User;
import mall.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static mall.core.session.SessionKey.LOGIN_USER;

@WebServlet("/client/order")
public class ClientOrderController extends Controller {
    private final OrderService service = new OrderService();

    /**
     * 前台订单页
     */
    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User loginUser = (User) req.getSession().getAttribute(LOGIN_USER);

        List<Order> orderList = service.list(loginUser.getId());

        req.setAttribute("orderList", orderList);
        forward("/client/order",req,resp);
    }

    // /**
    //  * 后台订单页
    //  */
    // public void admin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //     List<Order> list = service.list(new Order());
    //     req.setAttribute("list", list);
    //     req.getRequestDispatcher(req.getContextPath() + "/views/admin/list.jsp").forward(req, resp);
    // }


    /**
     * 新增订单
     */
    public void create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User loginUser = (User) req.getSession().getAttribute(LOGIN_USER);
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        service.create(loginUser.getId(),name,address,phone);

        redirect("/client/order?method=index",req,resp);
    }

    /**
     * 立即支付
     */
    public void pay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer orderId = Integer.valueOf(req.getParameter("orderId"));
        service.pay(orderId);

        redirect("/client/order?method=index",req,resp);
    }

    /**
     * 取消订单
     */
    public void cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer orderId = Integer.valueOf(req.getParameter("orderId"));
        service.cancel(orderId);
        redirect("/client/order?method=index",req,resp);
    }


    /**
     * 删除订单
     */
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // todo
        redirect("/client/order?method=index",req,resp);
    }


    /**
     * 修改订单
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // todo
        redirect("/client/order?method=index",req,resp);
    }


}
